package com.ideafactory.client.ybservice.xmpphandler;

import android.os.SystemProperties;

import com.ideafactory.client.afinal.NetConstants;
import com.ideafactory.client.utils.CommonUtils;
import com.ideafactory.client.utils.HandleMessageUtils;
import com.ideafactory.client.utils.HeartBeatClient;
import com.ideafactory.client.utils.ScreenUtils;
import com.ideafactory.client.utils.SdCardUtils;
import com.ideafactory.client.utils.VersionUpdateConstants;
import com.ideafactory.client.utils.location.DeviceLocation;
import com.ideafactory.client.utils.location.LocationBean;
import com.ideafactory.client.utils.netutils.MyCallBack;
import com.ideafactory.client.utils.netutils.NetUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LiuShao on 2016/3/4.
 */
public class MachineDetial {

    private String upMechineDetialUrl = NetConstants.RESOURSE_BASE_URL+"/device/service/updateDeviceHardwareInfo.html";

    private static MachineDetial machineDetial;

    public static MachineDetial getInstance(){
        if(machineDetial==null){
            machineDetial = new MachineDetial();
        }
        return machineDetial;
    }

    MachineDetial(){
        DeviceLocation.getDeviceLocation().getLocation();
    }

    /**
     * 上传设备信息
     */
    public void upLoadHardWareMessage() {
        HandleMessageUtils.getInstance().runInThread(new Runnable() {
            @Override
            public void run() {
                Map map = new HashMap();
                map.put("deviceNo", HeartBeatClient.getDeviceNo());
                map.put("screenWidth", String.valueOf(ScreenUtils.getScreenWidth()));
                map.put("screenHeight", String.valueOf(ScreenUtils.getScreenHeight()));
                map.put("diskSpace", SdCardUtils.getMemoryTotalSize());
                map.put("useSpace",SdCardUtils.getMemoryUsedSize());
                map.put("softwareVersion", CommonUtils.getVersionName() +"_"+ VersionUpdateConstants.CURRENT_VERSION);
                map.put("screenRotate", String.valueOf(SystemProperties.get("persist.sys.hwrotation")));
                map.put("deviceCpu", CommonUtils.getCpuName()+" "+CommonUtils.getNumCores()+"核"+CommonUtils.getMaxCpuFreq() +"khz");


                if(isLocationInited){
                    map.put("latitude", LocationBean.getAltitude());
                    map.put("longitude",LocationBean.getLongitude());
                    map.put("address",LocationBean.getAdress());
                    map.put("addressHeight",LocationBean.getAdressHeight());
                    map.put("cityName",LocationBean.getCity());
                }
                NetUtils.Post(upMechineDetialUrl, map, new MyCallBack<String>() {});
            }
        });
    }


    private LocationBean LocationBean;
    private boolean isLocationInited = false;
    public void setLocation(LocationBean LocationBean){
        this.LocationBean = LocationBean;
        isLocationInited = true;
        upLoadHardWareMessage();
        DeviceLocation.getDeviceLocation().stopLocation();
    }

}
